package com.yuexun.dhdoctor.module.Contract.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.adapter.GroupAdapter;
import com.yuexun.dhdoctor.module.Contract.bean.GroupGroupData;
import com.yuexun.dhdoctor.module.Contract.model.impl.GroupModel;
import com.yuexun.dhdoctor.module.Contract.presenter.impl.GroupPresenter;
import com.yuexun.dhdoctor.module.Contract.ui.Act_CreateGroup;
import com.yuexun.dhdoctor.module.Contract.ui.Act_ManageGroup;
import com.yuexun.dhdoctor.module.Contract.ui.view.IGroupView;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/7/17.
 */
public class Frag_Group extends Fragment implements IGroupView {

    @Bind(R.id.lvGroup)
    PullToRefreshBase lvGroup;
    private GroupPresenter presenter;
    private BaseAdapter adapter;
    private ListView lv;
    private Context context;

    ArrayList<GroupGroupData> groupGroupDataBeans = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = this.getActivity().getApplicationContext();
        View view = inflater.inflate(R.layout.frag_group, null);
        ButterKnife.bind(this, view);
        initView(view);
//        initData();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        presenter.getGroupDatas(params);
    }

    void initView(View view) {
        presenter = new GroupPresenter(new GroupModel(), this);
        lv = (ListView) lvGroup.getRefreshView();

        lvGroup.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                groupGroupDataBeans.clear();

                Map<String, String> params = new HashMap<>();
                params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
                presenter.getGroupDatas(params);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {

            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(context, Act_ManageGroup.class);
                bundle.putSerializable("groupinfo", groupGroupDataBeans.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void showGroupDatas(BaseEntity<GroupGroupData> groupGroupBean) {
        if (lvGroup.isPullRefreshEnabled()) {
            lvGroup.onPullDownRefreshComplete();
        }

        if ("200".equals(groupGroupBean.getStatus())) {
            if (groupGroupDataBeans.size() > 0) {
                groupGroupDataBeans.clear();
            }
            groupGroupDataBeans = groupGroupBean.getDatas();
            adapter = new GroupAdapter(getActivity(), groupGroupDataBeans);
            lv.setAdapter(adapter);
        }
    }

    @OnClick(R.id.btnCreate)
    public void onClick() {
        Intent intent = new Intent(context, Act_CreateGroup.class);
        startActivity(intent);
    }
}
